package com.why.controller;

import com.github.pagehelper.PageInfo;
import com.why.ann.MyAnn;
import com.why.pojo.*;
import com.why.pojo.vo.ManageVo;
import com.why.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author new wei
 * @date 2022/5/16 9:33
 */
@Controller
@RequestMapping("manage")
public class ManageController {
    @Autowired
    ManageService manageService;

    @RequestMapping("echars")
    @ResponseBody
    public List echars(){
        return manageService.listCount();
    }

    @GetMapping("toEchars")
    public String toEchars(){
        return "echars";
    }

    @MyAnn(value = "添加")
    @PostMapping("add")
    @ResponseBody
    public Map<String,String> add(Manage manage){
        HashMap<String, String> map = new HashMap<>();
        map.put("error","");
        List<Detail> detailList = manage.getDetailList();
        if (null==detailList){
            map.put("error","至少添加一条数据");
            return map;
        }
        for (Detail detail : detailList) {
            if (detail.getStartTime().isEmpty()||detail.getEndTime().isEmpty()||
                    detail.getPrice().isNaN()){
                map.put("error","价格明细不能有空数据");
                return map;
            }
        }
        int i=manageService.addManage(manage);
        map.put("success","保存成功");
        return map;
    }

    @MyAnn(value = "去添加页面")
    @GetMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    @GetMapping("getPid")
    @ResponseBody
    public List getPid(){
        List<Place> list=manageService.getPid();
        return list;
    }

    @GetMapping("getTid")
    @ResponseBody
    public List getTid(){
        List<Type> list=manageService.getTid();
        return list;
    }

    @GetMapping("getVid")
    @ResponseBody
    public List getVid(){
        List<Venue> list=manageService.getVid();
        return list;
    }

    @RequestMapping("list")
    public String list(Model model, ManageVo vo, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        PageInfo<Manage> pageInfo=manageService.list(vo,pageNum,pageSize);
        model.addAttribute("pg",pageInfo);
        model.addAttribute("vo",vo);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        return "list";
    }
}
